﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_OpenBankAccount : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void CheckAvailability()
    {
        var bankAccounts = from bankAccount in db.BankAccountBanks select bankAccount;
        foreach (BankAccountBank account in bankAccounts)
        {
            if (account.BankAccount.CurrencyId.ToString() == CurrencyList.SelectedValue && account.TypeId.ToString() == TypeList.SelectedValue)
            {
                CreateBankAccountButton.Enabled = false;
                break;
            }
            else
            {
                CreateBankAccountButton.Enabled = true;
            }
        }
    }

    protected void CurrencyList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CheckAvailability();
    }

    protected void CreateBankAccountButton_Click(object sender, EventArgs e)
    {
        BankAccount bankAccount = new BankAccount();
        bankAccount.CurrencyId = Convert.ToInt32(CurrencyList.SelectedValue);
        bankAccount.Amount = 0;
        db.BankAccounts.InsertOnSubmit(bankAccount);
        db.SubmitChanges();
        BankAccountBank bankAccountBank = new BankAccountBank();
        bankAccountBank.TypeId = Convert.ToInt32(TypeList.SelectedValue);
        bankAccountBank.BankAccountId = bankAccount.BankAccountId;
        db.BankAccountBanks.InsertOnSubmit(bankAccountBank);
        db.SubmitChanges();
        Response.Redirect("~/Admin/BankAccounts.aspx");
    }
    protected void CurrencyList_DataBound(object sender, EventArgs e)
    {
        CheckAvailability();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        CheckAvailability();
    }
}